package cn.edu.scau.cmi.wuyihan.client;

import java.util.ArrayList;
import java.util.List;
import cn.edu.scau.cmi.wuyihan.adapter.AdapterOfClass;
import cn.edu.scau.cmi.wuyihan.adapter.AdapterOfObject;
import cn.edu.scau.cmi.wuyihan.adapter.Target;
import cn.edu.scau.cmi.wuyihan.adapter.domain.Student;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
 
public class AdapterClient extends Application {

	public static void main(String[] args) {
		Application.launch(args);
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		GridPane gridPane = new GridPane();
		BorderPane borderPane1 = new BorderPane();
		BorderPane borderPane2 = new BorderPane();
		BorderPane borderPane3 = new BorderPane();
		BorderPane borderPane4 = new BorderPane();
		
		Button button1 = new Button("类适配器排序");
		Button button2 = new Button("对象适配器排序");
	
//		TextField inputTextField = new TextField();
		
		TextArea outputTextArea1 = new TextArea();
		outputTextArea1.setMaxSize(300, 150);
		outputTextArea1.setWrapText(true);
		TextArea outputTextArea2 = new TextArea();
		outputTextArea2.setMaxSize(300, 150);
		outputTextArea2.setWrapText(true);
		TextArea outputTextArea3 = new TextArea();
		outputTextArea3.setMaxSize(300, 150);
		outputTextArea3.setWrapText(true);
		TextArea outputTextArea4 = new TextArea();
		outputTextArea4.setMaxSize(300, 150);
		outputTextArea4.setWrapText(true);
		
		borderPane1.setCenter(outputTextArea1);
		borderPane2.setCenter(outputTextArea2);
		borderPane3.setCenter(outputTextArea3);
		borderPane4.setCenter(outputTextArea4);

		gridPane.setAlignment(Pos.CENTER);
		gridPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
		gridPane.setHgap(5.5);
		gridPane.setVgap(5.5);

		gridPane.add(new Label("类适配器模式"), 0, 0);
		gridPane.add(new Label("类适配器显示结果"), 1, 0);
		gridPane.add(borderPane1, 0, 1);
		gridPane.add(borderPane2, 1, 1);
		gridPane.add(button1, 1, 2);
		gridPane.add(new Label("对象适配器模式"), 0, 3);
		gridPane.add(new Label("对象适配器显示结果"), 1, 3);
		gridPane.add(borderPane3, 0, 4);
		gridPane.add(borderPane4, 1, 4);
		gridPane.add(button2, 1, 5);

		button1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				String value = outputTextArea1.getText();
				String[] split1 = value.split("\\n");
				Target target = new AdapterOfClass();
				List<Student> list = new ArrayList<>();
				for(int i = 0;i < split1.length; i++) {
					String[] split2 = split1[i].split("\\s");
					list.add(new Student(Integer.valueOf(split2[0]), split2[1]));
				}
				List sortStudentList = target.sortStudent(list);
				outputTextArea2.setText(null);
				for(int i = 0; i < sortStudentList.size(); i++) {
					Student student = (Student) sortStudentList.get(i);
					outputTextArea2.appendText("00" + student.getNum() + " " + student.getName() + "\n");
				}
			}
		});
		
		button2.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				String value = outputTextArea3.getText();
				String[] split1 = value.split("\\n");
				Target target = new AdapterOfObject();
				List<Student> list = new ArrayList<>();
				for(int i = 0;i < split1.length; i++) {
					String[] split2 = split1[i].split("\\s");
					list.add(new Student(Integer.valueOf(split2[0]), split2[1]));
				}
				List sortStudentList = target.sortStudent(list);
				outputTextArea4.setText(null);
				for(int i = 0; i < sortStudentList.size(); i++) {
					Student student = (Student) sortStudentList.get(i);
					outputTextArea4.appendText("00" + student.getNum() + " " + student.getName() + "\n");
				}
			}
		});

		Scene scene = new Scene(gridPane, 1000, 650);
		primaryStage.setTitle("适配器");
		primaryStage.setScene(scene);
		primaryStage.show();

	}
 
 
}
